#include "testmesh.h"
#include "mesh.h"
#include "node.h"
#include "cassert"
#include <QDebug>
#include <iostream>
#include <armadillo>

using namespace arma;

testMesh::testMesh()
{
}

void testMesh::teste1()
{
    mesh malha(2,2,0.0,0.0,0.5,0.5);
    node no;

    no = malha.getNode(1);

    assert(no.getPositionX() == 0.5);
}

void testMesh::teste2()
{
    mesh malha(2,2,0.0,0.0,0.5,0.5);

    element elemento;

    elemento = malha.getElement(1);
    int t = elemento.getNode(1)->getGlobalNumeration();
    //qDebug()<<t;
    assert( t== 1);
}

void testMesh::teste3()
{
    mesh malha(3,3,0.0,0.0,0.5,0.5);

    element elemento;

    elemento = malha.getElement(5);
    int t = elemento.getNode(1)->getGlobalNumeration();
    //qDebug()<<t;
    assert( t== 6);
}

void testMesh::teste4()
{
    mesh malha(2,2,0.0,0.0,0.5,0.5);

//    malha.setLGMatrix();
//    mat lg = malha.getLGMatrix();

//    cout<<lg;

}

void testMesh::teste5()
{
    mesh malha(2,2,0.0,0.0,0.5,0.5);
    node& no =malha.getNode(1);

    no.setBoundaryCondition(1,1,1,1,2.0,3.0,2.0,8.9);
    cout << malha.getNode(1).getBoundaryCondition();
}

void testMesh::teste6()//teste setando condiçoes de contorno
{
    mesh malha(2,2,0.0,0.0,0.5,0.5);

    int nNosX = malha.getNElementsX() + 1;
    int nNosY = malha.getNElementsY() + 1;

    for (int i = 1; i <= nNosX*nNosY ; i++)
    {

        if(malha.getNode(i).getPositionX() == 1.0)
        {
            malha.getNode(i).setBoundaryCondition(1,0,1,0,1.0,0.0,3.5,0.0);
            cout << i;
            cout << malha.getNode(i).getBoundaryCondition();

        }
    }
}

void testMesh::teste7()
{
    mesh malha(2,2,0.0,0.0,0.5,0.5);

    int nNosX = malha.getNElementsX() + 1;
    int nNosY = malha.getNElementsY() + 1;


    for (int i = 1; i <= nNosX*nNosY ; i++)
    {

        if(malha.getNode(i).getPositionX() == 1.0)
        {
            malha.getNode(i).setBoundaryCondition(1,1,0,0,2.5,2.9,0.0,0.0);
        }
    }


//    malha.setEQMatrix();


//    qDebug() << malha.getEQMatrix();
}

void testMesh::teste8()
{
    mesh malha(2,2,0.0,0.0,0.5,0.5);

    int nNosX = malha.getNElementsX() + 1;
    int nNosY = malha.getNElementsY() + 1;


    for (int i = 1; i <= nNosX*nNosY ; i++)
    {

        if(malha.getNode(i).getPositionX() == 1.0)
        {
            malha.getNode(i).setBoundaryCondition(1,1,0,0,2.5,2.9,0.0,0.0);
        }
    }


//    malha.setEQMatrix();
    malha.setGlobalMatrix();
}

void testMesh::teste9()
{
    mesh malha(2,2,0.0,0.0,0.5,0.5);

    int nNosX = malha.getNElementsX() + 1;
    int nNosY = malha.getNElementsY() + 1;


    for (int i = 1; i <= nNosX*nNosY ; i++)
    {

        if(malha.getNode(i).getPositionX() == 1.0)
        {
            malha.getNode(i).setBoundaryCondition(1,1,0,0,2.5,2.9,0.0,0.0);
        }
    }


//    malha.setEQMatrix();
    malha.setGlobalMatrix();
//    malha.setLGMatrix();
//    qDebug() << malha.getLGMatrix();
//    qDebug() << malha.getEQMatrix();
    qDebug() << malha.getGlobalMatrix().n_rows;
    qDebug() << malha.getGlobalMatrix().n_cols;
    cout << malha.getGlobalMatrix();

}

void testMesh::teste10()
{
    mesh malha(2,2,0.0,0.0,0.5,0.5);

    int nNosX = malha.getNElementsX() + 1;
    int nNosY = malha.getNElementsY() + 1;


    for (int i = 1; i <= nNosX*nNosY ; i++)
    {

        if(malha.getNode(i).getPositionX() == 1.0)
        {
            malha.getNode(i).setBoundaryCondition(1,1,0,0,2.5,2.9,0.0,0.0);
        }
    }


//    malha.setEQMatrix();
    malha.setGlobalMatrix();
//    malha.setLGMatrix();


    element elemento = malha.getElement(2);
    elemento.setJacobiano(-sqrt(3)/3,sqrt(3)/3);
    qDebug() << elemento.getJacobiano();

}

void testMesh::teste11()
{
    mesh malha(4,4,0.0,0.0,0.25,0.25);

    int nNosX = malha.getNElementsX() + 1;
    int nNosY = malha.getNElementsY() + 1;

    float x=0.0,y=0.0;
    for (int i = 1; i <= nNosX*nNosY ; i++)
    {
        x = malha.getNode(i).getPositionX();
        y = malha.getNode(i).getPositionY();
        if(x < 0.02)
        {
            malha.getNode(i).setBoundaryCondition(1,1,0,0,cos(datum::pi *y)/(datum::pi *datum::pi ),0.0,0.0,0.0);
        }else if(x > 0.98)
        {
            malha.getNode(i).setBoundaryCondition(1,1,0,0,-cos(datum::pi *y)/(datum::pi *datum::pi ),0.0,0.0,0.0);
        }else if(y < 0.02)
        {
            malha.getNode(i).setBoundaryCondition(1,1,0,0,cos(datum::pi *x)/(datum::pi *datum::pi ),0.0,0.0,0.0);
        }else if(y > 0.98)
        {
            malha.getNode(i).setBoundaryCondition(1,1,0,0,cos(datum::pi *x)/(datum::pi *datum::pi ),0.0,0.0,0.0);
        }else
        {
            malha.getNode(i).setBoundaryCondition(0,0,0,0,0.0,0.0,0.0,0.0);
        }
    }


    malha.setGlobalMatrixNumerationXY();
    malha.setGlobalMatrix();
    malha.setGlobalForce();

    mat globalMatrix = malha.getGlobalMatrix();
    mat globalForce = malha.getGlobalForce();

    qDebug()<<globalForce;
    vec deslocation = solve(globalMatrix,globalForce);
    qDebug()<<deslocation;
    malha.setNodeDeslocation(deslocation);


}

void testMesh::teste12()
{
    mesh malha(4,4,0.0,0.0,0.25,0.25);

    int nNosX = malha.getNElementsX() + 1;
    int nNosY = malha.getNElementsY() + 1;

    float x=0.0,y=0.0;
    for (int i = 1; i <= nNosX*nNosY ; i++)
    {
        x = malha.getNode(i).getPositionX();
        y = malha.getNode(i).getPositionY();
        if(x < 0.1)
        {
            malha.getNode(i).setBoundaryCondition(1,1,0,0,cos(datum::pi *y)/(datum::pi *datum::pi ),0.0,0.0,0.0);
        }else if(x>0.9)
        {
            malha.getNode(i).setBoundaryCondition(1,1,0,0,-cos(datum::pi *y)/(datum::pi *datum::pi ),0.0,0.0,0.0);
        }else if(y<0.1)
        {
            malha.getNode(i).setBoundaryCondition(1,1,0,0,cos(datum::pi *x)/(datum::pi *datum::pi ),0.0,0.0,0.0);
        }else if(y>0.9)
        {
            malha.getNode(i).setBoundaryCondition(1,1,0,0,cos(datum::pi *x)/(datum::pi *datum::pi ),0.0,0.0,0.0);
        }else
        {
            malha.getNode(i).setBoundaryCondition(0,0,0,0,0.0,0.0,0.0,0.0);
        }
    }


    malha.setGlobalMatrixNumerationXY();
    malha.setGlobalMatrix();

    mat a = malha.getElement(2).getJacobiano();


    qDebug() << a;
    qDebug() << a.i();
    qDebug() << det(a);

    a = malha.getElement(2).getLocalMatrix();

    qDebug() << a;
    qDebug() << a.i();
    qDebug() << det(a);

    a = malha.getGlobalMatrix();


    qDebug() << a;
    qDebug() << a.i();
    qDebug() << det(a);

}










